package com.ldc.commonres.views

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatDialog
import com.ldc.commonres.databinding.ResCommonLoadingViewBinding

class CommonDialogLoading(ctx: Context) : AppCompatDialog(ctx) {
    private var viewBinding: ResCommonLoadingViewBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ResCommonLoadingViewBinding.inflate(layoutInflater)
        // 设置背景透明
        window?.let {
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        }
        viewBinding?.let {
            setContentView(it.root)
        }
    }

    fun setText(str: String) {
        viewBinding?.tvName?.text = str
    }


    companion object {
        private var dialog: CommonDialogLoading? = null
        fun showDialog(context: Context, str: String = "加载中，请稍等") {
            dialog = CommonDialogLoading(context).apply {
                this.setText(str)
            }
            dialog?.show()
        }

        fun dismiss() {
            dialog?.dismiss()
        }
    }
}